package com.example.demo.mapper;

import com.example.demo.model.Customer;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
import org.springframework.data.repository.query.Param;

import javax.persistence.QueryHint;
import java.util.List;

import static org.hibernate.jpa.QueryHints.HINT_COMMENT;

public interface CustomerRepository extends JpaRepository<Customer, Long> {

    List<Customer> findByLastName(String lastName);

    List<Customer> findByFirstName(String firstName);

    List<Customer> findByFirstNameAndLastName(String firstName, String lastName);

    List<Customer> findByFirstNameOrLastName(String firstName, String lastName);

    @Query("select c from Customer c where c.lastName = ?1 order by c.id desc")
    Customer findByLastName2(String name);

    @Query("select c from Customer c where c.lastName = ?1")
    List<Customer> findByLastName3(String name, Sort sort);

    /**
     * 这里Param的值和=:后面的参数匹配，但不需要和方法名对应的参数值对应
     * =:加上变量名，这里是与方法参数中有@Param的值匹配的，而不是与实际参数匹配的
     *
     * @param n
     * @return
     */
    @Query("select c from Customer c where c.lastName = :name or c.firstName=:name ")
    Customer findByName(@Param("name") String n);

    @Query("select c from Customer c where c.lastName like %?1 ")
    Customer findByName2(@Param("name") String name);


    @QueryHints(value = {@QueryHint(name =HINT_COMMENT,value = "a query for pageable")})
    @Query("select c from Customer c where c.firstName=:name or c.lastName=:name")
    Page<Customer> findByName3(@Param("name") String name, Pageable pageable);

}